<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  install
 */

class m_install_actions_WebsiteAction extends f_core_Action
{
	public function execute()
	{
        $formValues = $this->getFormValues('form');

        $config = f_loader_Config::getInstance();

        $this->addToModel('selectedLang',$config->getParameter('default', 'defaultDocumentLang'));
        $this->addToModel('langs', f_locale_Manager::getIsoCodes());

        $frontThemes = m_backoffice_services_BackOfficeService::getInstance()->getFrontThemes();

        $this->addToModel('frontThemes', $frontThemes);

        if(!is_null($formValues))
        {
            $errors = $this->checkForm($formValues);

            if(count($errors) == 0)
            {
                $rootNodeService = m_backoffice_services_RootNodeService::getInstance();
               
                $config->setParameter('default', 'frontLocale', $formValues['displayLang']);
                $config->setParameter('default', 'backTheme', 'default');
                $config->setParameter('default', 'frontTheme', 'default');
                $config->save();
                
                $rootNodeService->createModuleRootNode('medias');
                $rootNodeService->createModuleRootNode('backoffice');
                $rootNode = $rootNodeService->createModuleRootNode('website');

                $website = m_website_documents_Website::createNew()	->setAuthor('pagizer')
                                                                    ->setLabel($formValues['label'])
                                                                    ->setUrl($formValues['url'])
                                                                    ->setDisplayLang($formValues['displayLang'])
                                                                    ->save();
                $rootNode->addWebsite($website)->save();

                f_core_Controller::getInstance()->redirect(URL_RELATIVE.'/bo/install/Complete');
            }
            else
            {
                $this->addToModel('errors', $errors);
                $this->addToModel('value', $formValues);
                $this->setView('input');
            }
        }
        else
        {
            $values = array();
            $values['url'] = URL_ABSOLUTE_WO_HTTP;
            $this->addToModel('value', $values);

            $this->setView('input');
        }
	}

	public function validate()
	{
        if(!f_database_Provider::checkConnect())
        {
            return false;
        }
        if(!m_users_services_UserService::getInstance()->hasAdminUser())
        {
            return false;
        }

        $documentProvider = f_document_Provider::getInstance();

		return $documentProvider->loadDocuments('modules_website/website')->count() == 0;
	}

	public function errorHandler()
	{
        if(!f_database_Provider::checkConnect())
        {
            $this->addToModel('error', 'db');
        }
        else
        {
            if(!m_users_services_UserService::getInstance()->hasAdminUser())
            {
                $this->addToModel('error', 'user');
            }
            else
            {
                $this->addToModel('error', 'already');
            }
        }

        $this->setView('error');
	}

    public function checkForm($formValues)
    {
        $errors = array();

        if($formValues['label'] == '' || $formValues['url'] == '')
        {
            $errors['required'] = true;
        }

        return $errors;
    }
}